import 'dart:convert';

import 'package:get/get.dart';
import 'package:school_assistant_m/api/course/course_api.dart';
import 'package:school_assistant_m/app/routes/app_pages.dart';
import 'package:school_assistant_m/store/login_store.dart';

class CourseController extends GetxController with StateMixin{
  //TODO: Implement CourseController
  LoginStore store = LoginStore();
  CourseApi courseApi = Get.put(CourseApi());
  bool isAdmin = false;
  String role = '';
  var id = ''.obs;
  List dataForm = [].obs;
  Map searchForm = {}.obs;
  Map result = {}.obs;

  void getUserInfo() async {
    isAdmin = await LoginStore().getAdmin();
    role = await LoginStore().getRole();
  }

  Future<void> getScoreList() async {
    Map res = await courseApi.getScoreList();
    print(res['results']['code']);
    if(res['results']['code']==200){
      dataForm = res['results']['data'];
      print('----------dataForm----------------');
      print(dataForm);
      change(dataForm,status: RxStatus.success());
    }else if(res['results']['code'] == 500){
      change(null, status: RxStatus.error('超时'));
    } else {
      change(null, status: RxStatus.error('网络错误'));
    }
  }

  Future<void> getScoreById(id) async {
    Map res = await courseApi.getScoreById(id);
    if (res['code']==200){
      Get.toNamed(Routes.COURSE_DETAIL);
    }
  }

  @override
  void onInit() {
    getUserInfo();
    getScoreList();
    super.onInit();
  }

}
